package com.ruoyi.an.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import com.ruoyi.an.domain.AnWechatUserMechanism;
import com.ruoyi.an.service.IAnWechatUserMechanismService;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.Threads;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.an.mapper.AnWechatUserMapper;
import com.ruoyi.an.domain.AnWechatUser;
import com.ruoyi.an.service.IAnWechatUserService;

/**
 * 用户数据Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-04
 */
@Service
public class AnWechatUserServiceImpl implements IAnWechatUserService {

    private static final Logger logger = LoggerFactory.getLogger(AnWechatUserServiceImpl.class);

    @Autowired
    private AnWechatUserMapper anWechatUserMapper;
    @Autowired
    private IAnWechatUserMechanismService anWechatUserMechanismService;

    /**
     * 查询用户数据
     *
     * @param wechatUserId 用户数据主键
     * @return 用户数据
     */
    @Override
    public AnWechatUser selectAnWechatUserByWechatUserId(Long wechatUserId) {
        return anWechatUserMapper.selectAnWechatUserByWechatUserId(wechatUserId);
    }

    @Override
    public AnWechatUser selectAnWechatUserByOpenId(String openId) {
        return anWechatUserMapper.selectAnWechatUserByOpenId(openId);
    }

    @Override
    public List<AnWechatUser> selectAnWechatUserByPhoneNumber(String phoneNumber)
    {
        return anWechatUserMapper.selectAnWechatUserByPhoneNumber(phoneNumber);
    }


    /**
     * 查询用户数据列表
     *
     * @param anWechatUser 用户数据
     * @return 用户数据
     */
    @Override
    public List<AnWechatUser> selectAnWechatUserList(AnWechatUser anWechatUser)
    {
        if (anWechatUser.getParams().size() > 0)
        {
            try
            {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                anWechatUser.setBTime(sdf.parse(anWechatUser.getParams().get("beginTime").toString()));
                anWechatUser.setETime(sdf.parse(anWechatUser.getParams().get("endTime").toString()));
            } catch (ParseException e)
            {
                logger.error("日期格式转换错误");
            }
        }
        List<AnWechatUser> anWechatUsers = anWechatUserMapper.selectAnWechatUserList(anWechatUser);
        //查询用户对应的推广信息
//        List<Long> wechatUserIds = anWechatUsers.stream().map(AnWechatUser::getWechatUserId).collect(Collectors.toList());
//        if (wechatUserIds.size() > 0) {
//            List<AnWechatUserMechanism> anWechatUserMechanisms = anWechatUserMechanismService.selectWechatUserMechanismByWechatUserIds(wechatUserIds);
//            anWechatUserMechanisms = anWechatUserMechanisms.stream().filter(item -> item.getIsDone() == 0L).collect(Collectors.toList());
//            for (AnWechatUser wechatUser : anWechatUsers) {
//                List<AnWechatUserMechanism> filterList = anWechatUserMechanisms.stream().filter(item -> item.getWechatUserId().equals(wechatUser.getWechatUserId())).collect(Collectors.toList());
//                if (filterList.size() > 0) {
//                    AnWechatUserMechanism anWechatUserMechanism = filterList.get(0);
//                    wechatUser.setScreeningScore(anWechatUserMechanism.getScreeningScore());
//                    wechatUser.setIsDone(anWechatUserMechanism.getIsDone());
//                    wechatUser.setIsMechanism(0L);
//                    wechatUser.setAuthTime(anWechatUserMechanism.getAuthTime());
//                    wechatUser.setScreeningSource(anWechatUserMechanism.getMechanismName());
//                } else {
//                    wechatUser.setIsDone(1L);
//                    wechatUser.setIsMechanism(1L);
//                }
//            }
//        }
        return anWechatUsers;
    }

    /**
     * 新增用户数据
     *
     * @param anWechatUser 用户数据
     * @return 结果
     */
    @Override
    public int insertAnWechatUser(AnWechatUser anWechatUser) {
        anWechatUser.setCreateTime(DateUtils.getNowDate());
        return anWechatUserMapper.insertAnWechatUser(anWechatUser);
    }

    /**
     * 修改用户数据
     *
     * @param anWechatUser 用户数据
     * @return 结果
     */
    @Override
    public int updateAnWechatUser(AnWechatUser anWechatUser) {
        anWechatUser.setUpdateTime(DateUtils.getNowDate());
        return anWechatUserMapper.updateAnWechatUser(anWechatUser);
    }

    /**
     * 批量删除用户数据
     *
     * @param wechatUserIds 需要删除的用户数据主键
     * @return 结果
     */
    @Override
    public int deleteAnWechatUserByWechatUserIds(Long[] wechatUserIds) {
        return anWechatUserMapper.deleteAnWechatUserByWechatUserIds(wechatUserIds);
    }

    /**
     * 删除用户数据信息
     *
     * @param wechatUserId 用户数据主键
     * @return 结果
     */
    @Override
    public int deleteAnWechatUserByWechatUserId(Long wechatUserId) {
        return anWechatUserMapper.deleteAnWechatUserByWechatUserId(wechatUserId);
    }
}
